﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System;
public class CSharpCallLua : MonoBehaviour {

	// Use this for initialization
	void Start () {
        LuaEnv luaenv = new LuaEnv();
        luaenv.DoString("require 'CSharpCallLua'");
        int a = luaenv.Global.Get<int>("a");
        print(a);
        string str = luaenv.Global.Get<string>("str");
        print(str);
        bool isDie = luaenv.Global.Get<bool>("isDie");
        print(isDie);

        //Person p = luaenv.Global.Get<Person>("person");
        //print(p.name + "-" + p.age + "-" + p.age2);

        //luaenv.Dispose();
        //IPerson p = luaenv.Global.Get<IPerson>("person");
        //print(p.name + "-" + p.age);
        //p.name = "siki.edu";
        //luaenv.DoString("print(person.name)");
        //p.eat(12, 34);
        //luaenv.Dispose();

        //list映射
        //List<int> list = luaenv.Global.Get<List<int>>("person");
        //foreach(object o in list)
        //{
        //    print(o);
        //}
        //luaenv.Dispose();

        //Dictionary映射
        //Dictionary<string, object> dict = luaenv.Global.Get<Dictionary<string, object>>("person");
        //foreach(string key in dict.Keys)
        //{
        //    print(key + "-" + dict[key]);
        //}

        //全局函数映射
        //Action act1 = luaenv.Global.Get<Action>("Add");
        //act1();
        //act1 = null;

        //有参数
        Add Add = luaenv.Global.Get<Add>("Add");
        int resa, resb;
        int temp=  Add(20,1,out resa,out resb);
        Debug.Log(temp);
        Debug.Log("resa::"+resa);
        Debug.Log("resb::"+resb);
        Add = null;

        luaenv.Dispose();
	}
    [CSharpCallLua]
    delegate int Add(int a, int b,out int resa,out int resb);

	//class Person
 //   {
 //       public string name;
 //       public int age;
 //       //age2Lua没有相应的字段这里只是做测试
 //       public int age2;
 //   }
  [CSharpCallLua]
 interface IPerson
    {
        string name { get; set; }
        int age { get; set; }
        void eat(int a, int b);
    }
}
